package com.searching.core;

import com.searching.config.searching_Config;
import com.searching.core.index.FileScan;
import com.searching.core.model.Condition;
import com.searching.core.model.Thing;
import com.searching.core.search.FileSearch;

import java.util.List;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * searching的调度器
 */
public class searchingManager {
    private FileSearch fileSearch;
    private FileScan fileScan;
    private ExecutorService executorService;

    public searchingManager(FileSearch fileSearch, FileScan fileScan) {
        this.fileScan = fileScan;
        this.fileSearch = fileSearch;
    }

    /**
     * 检索
     */
    public List<Thing> search(Condition condition) {
        //NOTICE 扩展
        return this.fileSearch.search(condition);
    }

    /**
     * 索引
     */
    public void buildIndex() {
        Set<String> directories = searching_Config.getInstance().getIncludePath();
        if (this.executorService == null) {
            this.executorService =
                    Executors.newFixedThreadPool(directories.size(), new ThreadFactory() {
                        private final AtomicInteger threadId = new AtomicInteger(0);


                        @Override
                        public Thread newThread(Runnable r) {
                            Thread
                                    thread = new Thread(r);
                            thread.setName("Thread-Scan-" + threadId.getAndIncrement());
                            return thread;
                        }
                    });
        }

        final CountDownLatch countDownLatch = new CountDownLatch(directories.size());

        countDownLatch.countDown();

        System.out.println("build index start......");
        for (String path : directories) {
            //this.fileScan.index(path);    //单线程方式

            this.executorService.submit(new Runnable() {

                @Override
                public void run() {
                    searchingManager.this.fileScan.index(path);
                    //当前任务完成，值 - 1
                }
            });
            try {
                /**
                 * 阻塞，直达任务数为0
                 */
                countDownLatch.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("build index complete......");


//            new Thread(new Runnable() {
//
//                @Override
//                public void run() {
//                    searchingManager.this.fileScan.index(path);
//                }
//            }).start();
        }
        //TODO ======================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================1cmd和这里多看下


    }
}
